package com.mdl.common.util.token;


import com.mdl.common.util.string.ObjectTranslate;
import com.mdl.common.util.string.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: yunnanguoji
 * File Name: TokenUtil
 * Package Name: com.dlszkj.mdlyunxin.common.util.token
 * Date: 2018/4/10  14:57
 * Copyright (c) 2018, tianyul All Rights Reserved.
 * token工具类
 */
public class TokenUtil {

    /**
     * 生成token
     * @param userid
     * @param business_tel
     * @return
     */
    public static String createToken(String userid, String business_tel) {
        String token = null;
        try {
            token = DesUtil.encrypt(userid+","+business_tel+","+System.currentTimeMillis(), "mdl-mondora");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 解析token信息
     * @param token
     * @return
     */
    public static Map<String, Object> decryptToken(String token) {
        Map<String, Object> map = new HashMap<>();
        try {
            if (!StringUtil.isEmpty(token)) {
                String str = DesUtil.decrypt(token, "mdl-mondora");
                String[] para = str.split(",");
                map.put("userid", para[0]);
                map.put("username", para[1]);
                map.put("time", para[2]);
                long time = Long.parseLong(para[2]);
                Date date = new Date(time);
                Date now = new Date();
                int diff = DateUtil.diffDay(now, date);
                if (diff >= 7 * 24 * 60 * 60 * 1000) {
                    map.put("overdue", true);
                } else {
                    map.put("overdue", false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取userid
     * @param req
     * @return
     */
    public static String getUserid(HttpServletRequest req) {
        String token = req.getHeader("token");
        if(StringUtil.isEmpty(token)){
            return null;
        }
        token = token.replaceAll("%2F", "/").replaceAll("%2B", "+").replaceAll("%3D", "=");
        Map<String, Object> map = decryptToken(token);
        if (map == null) {
            return null;
        }
        return ObjectTranslate.getString(map.get("userid"),null);
    }
    /**
     * 获取userid
     * @param req
     * @return
     */
    public static Long getUserId(HttpServletRequest req) {
        String token = req.getHeader("token");
        if(StringUtil.isEmpty(token)){
            return null;
        }
        token = token.replaceAll("%2F", "/").replaceAll("%2B", "+").replaceAll("%3D", "=");
        Map<String, Object> map = decryptToken(token);
        if (map == null) {
            return null;
        }
        return ObjectTranslate.getLong(map.get("userid"),0L);
    }

    /**
     * 判断token是否过期
     * @param req
     * @return
     */
    public static boolean isOverDue(HttpServletRequest req) {
        boolean def = true;// 出错认为过期
		String token = req.getHeader("token");
        token = token.replaceAll("%2F", "/").replaceAll("%2B", "+").replaceAll("%3D", "=");
        Map<String, Object> map = decryptToken(token);
        if (map == null) {
            return def;
        }
        return ObjectTranslate.getBoolean(map.get("overdue"), def);
    }

    /**
     * 解密id
     * @param id
     * @return
     */
    public static String decryptId(String id){
        //非空校验
        if(StringUtil.isEmpty(id)){
            return id;
        }
        id = id.replaceAll(" ", "+");
        Map<String, Object> map = decryptToken(id);
        if (map == null) {
            return null;
        }
        return ObjectTranslate.getString(map.get("userid"));
    }

    /**
     * 生成token-使用string的userid
     * @param userid
     * @return
     */
    public static String createToken(String userid) {
        String token = null;
        try {
            token = DesUtil.encrypt(userid+","+"12345678901"+","+System.currentTimeMillis(), "mdl-mondora");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

}
